<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Product;
use App\Transformers\ProductTransformer;

class ProductController extends Controller
{

    /**
     * 通过商品分类查询商品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function listWithCategoryId(Request $request) {
        $product_category_id = $request->input('product_category_id');
        $products = Product::where('product_category_id', $product_category_id)->paginate(10);
        $data['data'] = [
            'products' => $this->factalPaginator($products, new ProductTransformer())
        ];
        return $this->apiSuccess($data);
    }

    /**
     * 查询餐馆的商品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request) {
        $bistro_id = $request->input('bistro_id');
        $name = $request->input('name');

        $products = Product::where('bistro_id', $bistro_id)->name($name)->orderby('id', 'desc')->get();
        $data['data'] = [
            'products' => $this->fractalItems($products, new ProductTransformer())
        ];
        return $this->apiSuccess($data);
    }

    /**
     * 商品详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id) {
        $product = Product::findorfail($id);
        $data['data'] = [
            'product' => $this->fractalItem($product, new ProductTransformer())
        ];
        return $this->apiSuccess($data);
    }

}
